package com.qxzn.app.desktoprobot.app;

import android.content.Context;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.qxzn.app.api.greendao.bean.DaoSession;
import com.qxzn.app.desktoprobot.BuildConfig;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.DisplayUtil;
import com.qxzn.app.util.FileUtil;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.app.util.SystemUtil;
import com.tencent.bugly.Bugly;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.sdk.api.INeedLoginListener;

import org.greenrobot.greendao.database.Database;

import cn.jpush.android.api.JPushInterface;

public class MyApplication extends MultiDexApplication {

    public static MyApplication myApp;
    private Database db;
    private DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        myApp = this;
        AppUtil.showLog(DisplayUtil.px2dip(this, SystemUtil.getScreenWidth(this)) + "---" + DisplayUtil.px2dip(this, SystemUtil.getScreenHeight(this)));
        //BUGLY初始化，测试阶段第三个参数(isDebug)设置为true(可立即上报详细日志和Logcat输出)；发布应改为false
        Bugly.init(this, "123", BuildConfig.DEBUG);
        FileUtil.getBaseFilePath(this);
//        LeakCanary.install(this);
        initJpush();
        //涂鸦初始化
        initTuya();
    }

    private void initJpush() {
//        PushReceiver receiver = new PushReceiver();
//        IntentFilter filter = new IntentFilter();
//        filter.addAction("cn.jpush.android.intent.REGISTRATION");
//        filter.addAction("cn.jpush.android.intent.MESSAGE_RECEIVED");
//        filter.addAction("cn.jpush.android.intent.NOTIFICATION_RECEIVED");
//        filter.addAction("cn.jpush.android.intent.NOTIFICATION_OPENED");
//        filter.addAction("cn.jpush.android.intent.CONNECTION");
//        registerReceiver(receiver, filter);
        //极光推送
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        if (!TextUtils.isEmpty(SharePreferenceUtil.getUserName(this))) {
            JPushInterface.setAlias(this, 1, SharePreferenceUtil.getUserName(this));
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    private void initTuya() {
        TuyaHomeSdk.init(this);
        //涂鸦调试开关
        TuyaHomeSdk.setDebugMode(BuildConfig.DEBUG);
        //Session由于可能存在一些异常或者在一段时间不操作（45天）会失效掉，这时候需要退出应用，重新登陆获取Session。
        TuyaHomeSdk.setOnNeedLoginListener(new INeedLoginListener() {
            @Override
            public void onNeedLogin(Context context) {
                AppUtil.showLog("Session失效");
            }
        });
    }

}
